package cn.exrick.xboot.modules.transport.service.impl;

import cn.exrick.xboot.common.exception.XbootException;
import cn.exrick.xboot.modules.transport.entity.BigCustomerUsers;
import cn.exrick.xboot.modules.transport.entity.BigCustomers;
import cn.exrick.xboot.modules.transport.mapper.BigCustomerUsersMapper;
import cn.exrick.xboot.modules.transport.mapper.BigCustomersMapper;
import cn.exrick.xboot.modules.transport.service.BigCustomersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 大客户公司基本信息接口实现
 * @author gj
 */
@Slf4j
@Service
@Transactional
public class BigCustomersServiceImpl extends ServiceImpl<BigCustomersMapper, BigCustomers> implements BigCustomersService {

    @Autowired
    private BigCustomerUsersMapper bigCustomerUsersMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeBigCustomersById(Long id) {
        //有用户的大客户不能直接删除
        Long count = bigCustomerUsersMapper.selectCount(new QueryWrapper<BigCustomerUsers>().eq("customer_id", id)  );
        if (count > 0) {
            throw new XbootException("存在关联的大客户与用户信息，无法删除");
        }
        this.removeById(id);
    }
}